package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /**
     * 角色添加
     *
     * @param roleDto
     */
    @Override
    public void createRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtil.copyProperties(roleDto, role);

        roleMapper.insert(role);

    }


    /**
     * 分页
     *
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<RoleVo> findRolePage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<RoleVo> page = (Page<RoleVo>) roleMapper.findRolePage(roleDto);
        return PageResponse.of(page, RoleVo.class);
    }

    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId
     * @return
     */
    @Override
    public Set<String> findCheckedResources(Long roleId) {
        Set<String> resources = roleResourceMapper.findCheckedResources(roleId);
        return resources;
    }


    /**
     * 角色下拉框
     * @return
     */
    @Override
    public List<RoleVo> RoleSelect() {
        List<Role> role = roleMapper.RoleSelect();

        return BeanUtil.copyToList(role,RoleVo.class);

    }


    /**
     * 删除角色
     * @param roleId
     */
    @Override
    public void deleteRoleById(Long roleId) {

        // todo
        roleMapper.deleteRoleById(roleId);
    }


    /**
     * 修改
     * @param roleDto
     */
    @Override
    public void modify(RoleDto roleDto) {

        Role role=new Role();
        BeanUtil.copyProperties(roleDto,role);
        roleMapper.updateByPrimaryKeySelective(role);
        roleResourceMapper.deleteByRoleId(role.getId());

        if (ArrayUtil.isNotEmpty(roleDto.getCheckedResourceNos())){
            List<RoleResource> roleResourceList = Arrays.stream(roleDto.getCheckedResourceNos()).map(new Function<String, RoleResource>() {
                @Override
                public RoleResource apply(String resourceNo) {

                    return RoleResource.builder().roleId(role.getId())
                            .resourceNo(resourceNo)
                            .dataState("0")
                            .build();
                }
            }).collect(Collectors.toList());
            roleResourceMapper.batchInsert(roleResourceList);
        }





    }


}
